package luogu.dp;
//P1130 红牌

import java.util.Scanner;
//思路正确但是内存超限制（需升级）
/**
 * @author :chenjie
 * @date :Created 2022/10/15 22:37
 */
public class RedBrand {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[][]arr=new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j]=sc.nextInt();
            }
        }
        int temp=0;
        for (int j = 1; j <n; j++) {
            for (int i = 0; i < m; i++) {
                temp=arr[i][j]+arr[i][j-1];
                if(temp>arr[(i-1+m)%m][j-1]+arr[i][j]){
                    temp=arr[(i-1+m)%m][j-1]+arr[i][j];
                }
                arr[i][j]=temp;

            }
        }
        int min=arr[0][n-1];
        for (int i = 0; i < m; i++) {
            if(min>arr[i][n-1]){
                min=arr[i][n-1];
            }
        }
        System.out.println(min);
    }
}
